//===================================================头文件===================================================
#include "PWM.h" 



//===================================================函数===================================================
/*---------------------------------------------------------------
 【函    数】(1)PWM_init_T2_CH1_PA0
 【功    能】输出比较通道1使用PA0口输出PWM波
 【参    数】无
 【返 回 值】无
 【注意事项】
 ----------------------------------------------------------------*/
void PWM_init_T2_CH1_PA0(uint16_t ccr)
{
	//初始化PA0
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	GPIO_InitTypeDef GPIO_PA0_InitStructure;
	GPIO_PA0_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_PA0_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_PA0_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_PA0_InitStructure);

	//初始化定时器
	//PWM频率 = CK_PSC(72M)/(ARR+1)/(PSC+1)
	Time2_InternalClock_Up_init(20000,72);
	
	//配置输出比较单元CCR：
	TIM_OCInitTypeDef TIM_OCInitStructure;
	//给TIM2_CH1的初始化结构体先进行一次初始化把用不到的关于高级定时器的成员也初始化
	//然后再对要用到的成员变量进行更改
	TIM_OCStructInit(&TIM_OCInitStructure);
	//输出比较模式
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	//输出比较极性
	//高极性 = 极性不反转；低极性反之，这里选的高极性
	TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High;
	//输出使能
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	//CCR比较寄存器的值
	TIM_OCInitStructure.TIM_Pulse = ccr;//CCR
	
	TIM_OC1Init(TIM2, &TIM_OCInitStructure);

	
	//启动定时器
	TIM_Cmd(TIM2,ENABLE);
}
/*---------------------------------------------------------------
 【函    数】(2)PWM_set_T2_CH1_PA0
 【功    能】单独修改通道1的CCR的值
 【参    数】无
 【返 回 值】无
 【注意事项】
 ----------------------------------------------------------------*/
void PWM_set_T2_CH1_PA0(uint16_t compare)
{
	TIM_SetCompare1(TIM2,compare);//用于单独修改通道1的CCR值
}

/*---------------------------------------------------------------
 【函    数】(3)PWM_init_T2_CH2_PA1
 【功    能】输出比较通道2使用PA1口输出PWM波
 【参    数】无
 【返 回 值】无
 【注意事项】
 ----------------------------------------------------------------*/
void PWM_init_T2_CH2_PA1(uint16_t ccr)
{
	//初始化PA1
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	GPIO_InitTypeDef GPIO_PA1_InitStructure;
	GPIO_PA1_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_PA1_InitStructure.GPIO_Pin = GPIO_Pin_1;
	GPIO_PA1_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_PA1_InitStructure);

	//初始化定时器
	//PWM频率 = CK_PSC(72M)/(ARR+1)/(PSC+1)
	Time3_InternalClock_Up_init(20000,72);
	
	//配置输出比较单元CCR：
	TIM_OCInitTypeDef TIM_OCInitStructure;
	//给TIM2_CH1的初始化结构体先进行一次初始化把用不到的关于高级定时器的成员也初始化
	//然后再对要用到的成员变量进行更改
	TIM_OCStructInit(&TIM_OCInitStructure);
	//输出比较模式
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	//输出比较极性
	//高极性 = 极性不反转；低极性反之，这里选的高极性
	TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High;
	//输出使能
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	//CCR比较寄存器的值
	TIM_OCInitStructure.TIM_Pulse = ccr;//CCR
	
	TIM_OC2Init(TIM2, &TIM_OCInitStructure);

	
	//启动定时器
	TIM_Cmd(TIM2,ENABLE);
}
/*---------------------------------------------------------------
 【函    数】(4)PWM_set_T2_CH2_PA1
 【功    能】单独修改通道2的CCR的值
 【参    数】无
 【返 回 值】无
 【注意事项】
 ----------------------------------------------------------------*/
void PWM_set_T2_CH2_PA1(uint16_t compare)
{
	TIM_SetCompare2(TIM2,compare);//用于单独修改通道2的CCR值
}
/*---------------------------------------------------------------
 【函    数】(3)PWM_init_T2_CH2_PA1
 【功    能】输出比较通道2使用PA1口输出PWM波
 【参    数】无
 【返 回 值】无
 【注意事项】
 ----------------------------------------------------------------*/
void PWM_init_T3_CH1_PA6(uint16_t ccr)
{
	//初始化PA6
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	GPIO_InitTypeDef GPIO_PAx_InitStructure;
	GPIO_PAx_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_PAx_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_PAx_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_PAx_InitStructure);


	
	//初始化定时器
	//PWM频率 = CK_PSC(72M)/(ARR+1)/(PSC+1)
	Time3_InternalClock_Up_init(1000,5);
	

	
	//配置输出比较单元CCR：
	TIM_OCInitTypeDef TIM_OCInitStructure;
	//给TIM2_CH1的初始化结构体先进行一次初始化把用不到的关于高级定时器的成员也初始化
	//然后再对要用到的成员变量进行更改
	TIM_OCStructInit(&TIM_OCInitStructure);
	//输出比较模式
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	//输出比较极性
	//高极性 = 极性不反转；低极性反之，这里选的高极性
	TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High;
	//输出使能
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	//CCR比较寄存器的值
	TIM_OCInitStructure.TIM_Pulse = ccr;//CCR
	
	TIM_OC1Init(TIM3, &TIM_OCInitStructure);

	
	//启动定时器
	TIM_Cmd(TIM3,ENABLE);
}

/*---------------------------------------------------------------
 【函    数】(6)PWM_set_T3_CH1_PA6(uint16_t compare)
 【功    能】单独修改通道2的CCR的值
 【参    数】无
 【返 回 值】无
 【注意事项】
 ----------------------------------------------------------------*/
void PWM_set_T3_CH1_PA6(uint16_t compare)
{
	TIM_SetCompare1(TIM3,compare);//用于单独修改通道1的CCR值
}
/*---------------------------------------------------------------
 【函    数】(7)PWM_init_T3_CH2_PA7(uint16_t ccr)
 【功    能】定时器3输出比较通道2使用PA7口输出PWM波
 【参    数】无
 【返 回 值】无
 【注意事项】
 ----------------------------------------------------------------*/
void PWM_init_T3_CH2_PA7(uint16_t ccr)
{
	//初始化PA7
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	GPIO_InitTypeDef GPIO_PAx_InitStructure;
	GPIO_PAx_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_PAx_InitStructure.GPIO_Pin = GPIO_Pin_7;
	GPIO_PAx_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_PAx_InitStructure);


	
	//初始化定时器
	//PWM频率 = CK_PSC(72M)/(ARR+1)/(PSC+1)
	Time3_InternalClock_Up_init(100,36);
	

	
	//配置输出比较单元CCR：
	TIM_OCInitTypeDef TIM_OCInitStructure;
	//给TIM2_CH1的初始化结构体先进行一次初始化把用不到的关于高级定时器的成员也初始化
	//然后再对要用到的成员变量进行更改
	TIM_OCStructInit(&TIM_OCInitStructure);
	//输出比较模式
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	//输出比较极性
	//高极性 = 极性不反转；低极性反之，这里选的高极性
	TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High;
	//输出使能
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	//CCR比较寄存器的值
	TIM_OCInitStructure.TIM_Pulse = ccr;//CCR
	
	TIM_OC2Init(TIM3, &TIM_OCInitStructure);

	
	//启动定时器
	TIM_Cmd(TIM3,ENABLE);
}

/*---------------------------------------------------------------
 【函    数】(8)PWM_set_T3_CH2_PA7(uint16_t compare)
 【功    能】单独修改定时器3通道2的CCR的值
 【参    数】无
 【返 回 值】无
 【注意事项】
 ----------------------------------------------------------------*/
void PWM_set_T3_CH2_PA7(uint16_t compare)
{
	TIM_SetCompare2(TIM3,compare);//用于单独修改通道2的CCR值
}
